Advent of Code - Day 8: Treetop Tree House

8th day of the advent of code using R

advent of code
Published

December 8, 2022

Part 1

library(readr)

Input

input <-   as.matrix(read_fwf("input.txt",fwf_widths(widths = rep(1,99)))) 

Part 1

Code

is_visible <- function(x){
  results <- vector(mode = "logical", length = length(x))
  
  for (i in seq_along(x)) {
    if(i == 1 || i == length(x)){
      results[i] <- TRUE
    } else if (all(x[i] > x[1:i-1]) || all(x[i] > x[(i+1):length(x)])) {
      results[i] <- TRUE
    } else {
      results[i] <- FALSE
    }
  }
  results
  
}

sum(t(apply(input, 1, is_visible)) | apply(input, 2, is_visible))
[1] 1840

Part 2

scenic_score <- function(x){
  right <- vector(mode = "numeric", length = length(x))
  left <- vector(mode = "numeric", length = length(x))
  
  for (i in seq_along(x)) {
    if (i == 1) {
      left[i] <- 0
    } else {
      left_dist <- which(x[(i-1):1] >= x[i])[1]
      left[i] <- ifelse(is.na(left_dist), i-1, left_dist)
    }
    if(i==length(x)){
      right[i] <- 0
    } else {
      right_dist <- which(x[(i+1):length(x)] >= x[i])[1]
      right[i] <- ifelse(is.na(right_dist), length(x) - i, right_dist)
    }
  }
  
  left*right
}

max(t(apply(input, 1, scenic_score)) * apply(input, 2, scenic_score))
[1] 405769